package com.bitoon.basketdudes.web.jpa;

import java.io.Serializable;
import javax.persistence.*;

import org.apache.log4j.Logger;

@Entity
@Table(name = "ranking", schema="rankings")
@NamedQueries({
    @NamedQuery(name = "Ranking.findAll",
        query = "SELECT r FROM Ranking r WHERE r.posicion > 0 ORDER BY r.posicion"),
    @NamedQuery(name = "Ranking.findByNombreEquipo",
        query = "SELECT r FROM Ranking r WHERE r.posicion > 0 AND LOWER(r.equipo.nombre) LIKE :nombreEquipo ORDER BY r.posicion"),
    @NamedQuery(name = "Ranking.findByNombreManager", 
        query = "SELECT r FROM Ranking r WHERE r.posicion > 0 AND LOWER(r.equipo.usuario.nick) LIKE :nombreManager ORDER BY r.posicion"),
    @NamedQuery(name = "Ranking.findById",
        query = "SELECT r FROM Ranking r WHERE r.id = :id")
    /*
    @NamedQuery(name = "Ranking.findByPosicion", query = "SELECT r FROM Ranking r WHERE r.posicion = :posicion"),
    @NamedQuery(name = "Ranking.findByAntPosicion", query = "SELECT r FROM Ranking r WHERE r.antPosicion = :antPosicion")
     *
     */
})
public class Ranking implements Serializable {
    private static final long serialVersionUID = 1L;

    @Id
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;

    @Basic(optional = false)
    @Column(name = "posicion")
    private Integer posicion;
    @Basic(optional = false)
    @Column(name = "ant_posicion")
    private Integer antPosicion;

    @ManyToOne
    @JoinColumn(name="id", insertable=false, updatable=false)
    private EquipoRanking equipo;

    public Ranking() {
    }

    public Ranking(Integer id) {
        this.id = id;
    }

    public Ranking(Integer id, int posicion, int antPosicion) {
        this.id = id;
        this.posicion = posicion;
        this.antPosicion = antPosicion;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public int getPosicion() {
        return posicion;
    }

    public void setPosicion(int posicion) {
        this.posicion = posicion;
    }

    public int getAntPosicion() {
        return antPosicion;
    }

    public void setAntPosicion(int antPosicion) {
        this.antPosicion = antPosicion;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Ranking)) {
            return false;
        }
        Ranking other = (Ranking) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "com.bitoon.basketdudes.jpa.web.Ranking[id=" + id + "]";
    }

    public EquipoRanking getEquipo() {
        return equipo;
    }

    public void setEquipo(EquipoRanking equipo) {
        this.equipo = equipo;
    }

    public int getVariacion() {
        return antPosicion - posicion;
    }

}
